<template>
	<view>
		<view v-for="(item,index) in sr.tpl" :key="index">
      <rich-text class="csmwj-group-line z-item" v-if="item.__config__.tag=='csmwj-group-line'" :nodes="item.csmwjtext.value"></rich-text>
      
      <view class="csmwj-group-line z-item" v-if="item.__config__.tag=='csmwj-group-line'">{{item.csmwjtext.value}}</view>
      <view class="csmwj-scoring z-item" v-if="item.__config__.tag=='csmwj-scoring'">{{item.csmwjtext.value}}<text class="red" style="padding-left: 14rpx;">总分 : {{item.scoreAll}}</text></view>
      <!-- zxbVal-{{item.zxbVal}} -->
			<c-parsefg-csmwj-header v-if="item.__config__.tag=='csmwj-header'" :field="item" :paper="paper" />
      <!-- --{{item.id}} -->
      
			<c-parsefg-csmwj-footer v-if="item.__config__.tag=='csmwj-footer'" :field="item" :paper="paper"/>
      <view style="width: 100%; height: 20rpx;"></view>
			<c-parsefg-csmwj-input v-if="item.__config__.tag=='csmwj-input' || item.__config__.tag=='csmwj-input-score' || item.__config__.tag=='csmwj-input-fixed-score'" ref="parseitem" :field="item" :paper="paper" :index="item.excludeheaderendIndex" @res="getZxbVal($event, index)"/>
			<c-parsefg-csmwj-checkbox-group v-if="item.__config__.tag=='csmwj-checkbox-group'" ref="parseitem" :field="item" :paper="paper" :index="item.excludeheaderendIndex"/>
      <template v-if="item.__config__.tag=='csmwj-radio-group'">
        <c-parsefg-csmwj-radio-group  @res="getZxbVal($event, index)" v-if="item.__config__.csmtype=='csmwj-score'" ref="parseitem" :field="item" :paper="paper" :index="item.excludeheaderendIndex" />
        <c-parsefg-csmwj-radio-group v-else ref="parseitem" :field="item" :paper="paper" :index="item.excludeheaderendIndex"/>
      </template>
			
			
		</view>
		
	</view>
</template>

<script>

	export default {
		name: 'c-parsefg',
		props: {
			tpl: {
				type: Array,
				default: []
			},
			paper: {
				type: Object
			},
			doing_userpaper:{
				type: Object
			},
		},
		data() {
			return {
				sr: {
					tpl: [],
				},
        scoreList:[]
			};
		},
		mounted() {
			let that = this;
			that.cinit();
			console.log('sr----》', this.sr)
      let tpl = JSON.parse(JSON.stringify(that.tpl))
			//计算序号,需要跳过welcome和end的序号
      let scoreCurIdx = -1;
      let scoreList = [];
      let item = {pIdx:-1, list:[]}
			if(true){
				let excludeheaderendIndex = 0;
				for(let index in tpl){
          tpl[index].zxbVal = 0
          tpl[index].is_dfGrop = false
          tpl[index].is_score = false
					if(tpl[index].__config__.tag!='csmwj-header' && tpl[index].__config__.tag!='csmwj-footer'){
						excludeheaderendIndex++;
						tpl[index].excludeheaderendIndex = excludeheaderendIndex;
					}else{
						tpl[index].excludeheaderendIndex = 0;
					}
          if(tpl[index].__config__.csmtype==='csmwj-scoring'){
            scoreCurIdx = Number(index);
            tpl[index].scoreAll = 0;
            tpl[index].scoreList = []
          }
          
          // console.log('inasdas', scoreCurIdx, Number(index), tpl[index].__config__.csmtype, scoreCurIdx < Number(index))
          if(scoreCurIdx > -1 && scoreCurIdx < Number(index)){
            if(tpl[index].__config__.csmtype === 'csmwj-score' || tpl[index].__config__.csmtype === 'csmwj-input-score' || tpl[index].__config__.csmtype === 'csmwj-input-fixed-score'){
              tpl[index].is_dfGrop = true; // 是不是打分组的数据
              tpl[index].dfPid = tpl[scoreCurIdx].id; // 打分组的组别id
              tpl[scoreCurIdx].scoreList.push({pIdx:scoreCurIdx, curIdx:index, id: tpl[index].id})
            }
          }
          
				}
				that.sr.tpl = tpl;
        tpl.forEach((n, i)=>{
          if(n.scoreList){
            scoreList.push(...n.scoreList)
          }
        })
        that.scoreList = scoreList
        console.log('tpl--->',scoreList)
			}
		},
		methods: {
			cinit:function(){
				let that = this;	
			},
			// 如果校验通过则返回值,如果没有通过则返回false
			__submit1:function(){
				let that = this;
				let ret = true;
				let values = [];
				for(let index in that.$refs.parseitem){
					let item = that.$refs.parseitem[index];
          // console.log('__submit----item--->', item)
					let ret2 = item.__submit();
					if(ret2===false){
						ret = false;
					}
					values[index] = {id:item.field.id,__answer__:ret2};
				}
				if(ret===false){
					return false;
				}else{
          console.log('values=--->', values)
					return values;
				}
			},
      __submit(){
        let values = this.__submit1()
        let scoreList = [];
        let scoreListAll = [];
        
        if(this.scoreList){
          let is_dfGropuList = this.sr.tpl.filter(n => n.is_dfGrop)
          let scoreListItem = [];
          let noList = []
          let dfPid = -1;
          let is_dfGropuList_num = 0
          console.log('is_dfGropuList---', is_dfGropuList)
          values.forEach((n, ni)=>{
            let m1 = is_dfGropuList.find(j=>j.id == n.id)
            if(m1){
              // is_dfGropuList_num++
              // if(dfPid == -1){
              //   dfPid = m1.dfPid
              // }
              // console.log('m1--->', n.id, m1.id, n.__answer__, m1.zxbVal, dfPid,m1.dfPid, is_dfGropuList_num, is_dfGropuList.length)
              // if(dfPid === m1.dfPid){
              //   scoreListItem.push({...n, is_score:true})
              // }else if(dfPid !== m1.dfPid){
              //   dfPid = m1.dfPid
              //   scoreList.push(scoreListItem)
              //   scoreListItem = []
              //   scoreListItem.push({...n, is_score:true})
              //   console.log('中间量')
              // }
              scoreListItem.push({...n, is_score:true})
              
            }else{
              scoreListAll.push({...n,is_score:false})
            }
            
          })
          // if(is_dfGropuList_num == is_dfGropuList.length){
          //   console.log('最后一批')
          //   scoreList.push(scoreListItem)
          //   scoreListItem = []
          // }
          const other = this.getABDiff(scoreListAll, values)
          return {values:values, newvals:[...scoreListItem, ...scoreListAll]}
        }else{
          const other = this.getABDiff(scoreListAll, values)
          return {values:values, newvals:[...scoreListItem, ...scoreListAll]}
        }
        
        console.log('__submit---scoreList----', scoreList)
      },
      getABDiff(a, b){
        var filteredB = b.filter(item => !a.some(obj => obj.id === item.id));
        return filteredB
      },
      getZxbVal(e, index){
        // console.log('getZxbVal--------', e, index)
        if(this.scoreList){
          let tpl = JSON.parse(JSON.stringify(this.sr.tpl))
          let scoreAll = 0;
          let pIdx = -1;
          tpl[index].zxbVal = e;
          tpl.forEach(n1=>{
            scoreAll = 0
            let item = this.scoreList.find(n2=> n2.curIdx == index)
            if(item){
              let items = this.scoreList.filter(n3=> n3.pIdx == item.pIdx)
              items.forEach(n2=>{
                scoreAll+=Number(tpl[n2.curIdx].zxbVal)
              })
              tpl[item.pIdx].scoreAll = scoreAll
            }
          })
          this.sr.tpl = tpl
        }
        
      },
			__getVal:function(){
        console.log('__getVal', e, index)
				let that = this;
				let ret = true;
				let values = [];
				for(let index in that.$refs.parseitem){
					let item = that.$refs.parseitem[index];
					let ret2 = item.__getVal();
					values[index] = {id:item.field.id,__answer__:ret2};
				}
				return values;
			}
		}
	};
</script>

<style scoped>
  .z-item{padding: 0 30rpx; font-weight: bold;}
  .red{color: red;}
</style>
<!--
 * component yaoqiu:
 1. 必须由__submit方法
 2. 需要有view#errormsg,提供  _setErrorMsg和_setOkMsg方法 
 -->